﻿using LY.Manage.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LY.Manage.WebApp.App_Start;
using System.Text;
using LY.Manage.BLL;
namespace LY.Manage.WebApp.Controllers
{
	
    public class DemoInfoController : BaseController
    {
        
        

        //模板信息
        // GET: /DemoInfo//

        IBLL.IDemoInfoService DemoInfoService =new BLL.DemoInfoService();
        IBLL.IDepartmentService DepartmentService = new BLL.DepartmentService();
        public ActionResult Index()
        {
            return View();
        }
        #region 获取列表数据 GetDataInfoList
        public ActionResult GetDataInfoList()
        {
            //如果异常记录日志抛出去
            int pageIndex = Request["page"] != null ? int.Parse(Request["page"]) : 1;
            int pageSize = Request["rows"] != null ? int.Parse(Request["rows"]) : 10;

            int totalCount = 0;
            var dataList = DemoInfoService.LoadPageEntities<int>
                (pageIndex, pageSize,
                out totalCount,
                // u => u.accountName == objectId,
                u => u.delflag == 0,
                u => u.id,
                true
                ).ToList();
            //var temp = from r in dataList.ToList()
            //           select new
            //           {
            //               ID = r.ID,
            //               DepName = r.DepName,
            //               ParentId = GetName(r.ParentId),
            //               TreePath = r.TreePath,
            //               Level = r.Level
            //           };

       
            string result = "";
            ResultData<DemoInfo> resultdata = new ResultData<DemoInfo>()
            {
               
                code = 0,
                count = totalCount,
                msg = "",
                data = dataList

            };
            return Json(resultdata, JsonRequestBehavior.AllowGet);
            result = Newtonsoft.Json.JsonConvert.SerializeObject(resultdata);
            return Content(result);


        }

        public string GetName(int parentCode)
        {
            string result = "";
            var data = DepartmentService.LoadEntities(u => u.ID == parentCode).FirstOrDefault();

            if (data == null)
                return result;
            result = data.DepName;

            return result;
        }

        #endregion



        #region 显示添加数据页面 ShowAddInfo
        public ActionResult ShowAddInfo()
        {
            ParentDropDownList();
            return View();


        }

        #endregion
        #region 添加数据方法 AddInfo

        public ActionResult AddInfo(DemoInfo entity)
        {
            //添加默认数据

          
           WsResult ws = new WsResult();
           entity.depid = int.Parse(Request.Form["DepName"]);
           entity.currenttime = DateTime.Now;
           entity.delflag = 0;
           DemoInfoService.AddEntity(entity);
           ws.IsSuccess = true;
          ws.Msg = "添加成功!";
            return Json(ws);

        }
        #endregion
        #region 显示编辑页面 ShowEditInfo
        public ActionResult ShowEditInfo()
        {
            int id = int.Parse(Request["id"]);
            var model = DemoInfoService.LoadEntities(u => u.id == id && u.delflag == 0).FirstOrDefault();
            ParentDropDownList();
            return View(model);

        }
        #endregion
        #region 编辑数据方法 EditInfo
        public ActionResult EditInfo(DemoInfo entity)
        {

           WsResult ws = new WsResult();
            //1.根据主键ID查询数据
            var oldEntity = DemoInfoService.LoadEntities(u => u.id == entity.id && u.delflag == 0).FirstOrDefault();
            if (oldEntity == null)
            {
                ws.Msg = "编辑的ID不存在!";
                return Json(ws);
            }
            #region 2.设置要修改的数据


            oldEntity.name = entity.name;
            oldEntity.email = entity.email;
            oldEntity.sex = entity.sex;
            oldEntity.age = entity.age;
            oldEntity.birthday = entity.birthday;
            oldEntity.depid = entity.depid;
            oldEntity.isenable = entity.isenable;
			
            #endregion


            //3.更新数据，并返回
            if (DemoInfoService.UpdateEntity(oldEntity))
            {
                ws.IsSuccess = true;
                return Json(ws);
            }
            else
            {
                 ws.Msg = "更新失败!";
                return Json(ws);
            }
        }
        #endregion
        #region 删除数据方法 DeleteInfo
        public ActionResult DeleteInfo()
        {


             WsResult ws = new WsResult();

            if (Request["strId"] == null)
            {
                  ws.Msg = "id必须为整形!";
                return Json(ws);
            }

            string strId = Request["strId"];
            int id = Convert.ToInt32(strId);
            var entity = DemoInfoService.LoadEntities(u => u.id == id).FirstOrDefault();
            if (entity == null)
            {
                 ws.Msg = "删除的记录不存在!";
                return Json(ws);
            }
            //验证是否已经引用,如果引用则不能删除

            entity.delflag = 1;
           // entity.updatetime = DateTime.Now;
            //将list集合存储的要删除的记录的编号传递到业务层
            //这里采用更改标志的方式进行删除 ,
            if (DemoInfoService.UpdateEntity(entity))
            {
                 ws.IsSuccess = true;
                return Json(ws);
            }
            else
            {
                 ws.Msg = "更新失败!";
                return Json(ws);
            }
        }
        #endregion
        #region 显示详细页面 ShowEditInfo
        public ActionResult ShowDetailInfo()
        {
            int id = int.Parse(Request["id"]);

            var model = DemoInfoService.LoadEntities(u => u.id == id && u.delflag == 0).FirstOrDefault();

            return View(model);

        }
        #endregion


        public void ParentDropDownList()
        {
            //1.1查询YzSeriesEntity的数据  
            List<Model.Department> seriesList = DepartmentService.LoadEntities(u => u.ParentId == 0).ToList();
            List<SelectListItem> selectList = new List<SelectListItem>();

            foreach (var plitem in seriesList)
            {
                string parentName = plitem.DepName;
                string parentId = plitem.ID.ToString();
                selectList.Add(new SelectListItem
                {
                    Text = "├-" + parentName,
                    Value = parentId
                });

                int pid = plitem.ID;
                string tag = "　|- ";
                ChildrenList(pid, tag, selectList);
            }
            ViewBag.DevNameList = new SelectList(selectList, "Value", "Text");



        }
        public void ChildrenList(int pid, string tag, List<SelectListItem> selectList)
        {
            StringBuilder strWhere = new StringBuilder();
            // var childrenList = bll.GetModelList(strWhere.ToString()).Where(d => d.ParentId == pid);
            var childrenList = DepartmentService.LoadEntities(u => u.ParentId == pid).ToList();

            foreach (var clitem in childrenList)
            {
                string clname = tag + clitem.DepName;
                string clid = clitem.ID.ToString();
                selectList.Add(new SelectListItem
                {
                    Text = clname,
                    Value = clid
                });
                int pid2 = clitem.ID;
                string tag2 = tag + " -- ";
                ChildrenList(pid2, tag2, selectList);
            }
        }





      
    }
}

